package org.celery.client.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.celery.client.service.ClientOrderService;
import org.celery.modules.goods.entity.GoodsPackage;
import org.celery.modules.goods.entity.GoodsStyle;
import org.celery.modules.goods.service.IGoodsPackageService;
import org.celery.modules.other.entity.LogisticsFee;
import org.celery.modules.other.service.ILogisticsFeeService;
import org.celery.modules.user.service.IUserDomainService;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.User;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@Service
public class ClientOrderServiceImpl implements ClientOrderService {

    private final IGoodsPackageService goodsPackageService;
    private final ILogisticsFeeService logisticsFeeService;
    private final IUserDomainService userDomainService;

    @Override
    public BigDecimal computeTotalAmount(String associatedGoodsString) {
        List<GoodsStyle> goodsStyleList = JsonUtil.parseArray(associatedGoodsString, GoodsStyle.class);


        int packageGoodsCount = 0;
        GoodsPackage goodsPackage = new GoodsPackage();

        List<GoodsPackage> goodsPackageList = new ArrayList<>();
        if (Func.isNotEmpty(goodsStyleList)) {
            goodsPackageList = goodsPackageService.list(Wrappers.<GoodsPackage>lambdaQuery()
                    .eq(GoodsPackage::getGoodsId, goodsStyleList.get(0).getGoodsId())
            );
        }

        if (Func.isNotEmpty(goodsPackageList)) {
            for (GoodsPackage item : goodsPackageList) {
                if (goodsStyleList.size() >= item.getGoodsCount() && item.getGoodsCount() >= packageGoodsCount) {
                    packageGoodsCount = item.getGoodsCount();
                    goodsPackage = item;
                }
            }
        }

        BigDecimal totalAmount = BigDecimal.valueOf(0);
        for (int index = 0; index < goodsStyleList.size(); index++) {
            if (packageGoodsCount != 0) {
                if (packageGoodsCount >= index + 1) {
                    totalAmount = goodsPackage.getPrice();
                } else {
                    totalAmount = totalAmount.add(goodsStyleList.get(index).getPrice());
                }
            } else {
                totalAmount = totalAmount.add(goodsStyleList.get(index).getPrice());
            }
        }
        return totalAmount;
    }

    @Override
    public BigDecimal computeLogisticsFee(String address, String domain) {
        User user = userDomainService.selectUserByDomain(domain);
        if (Func.isEmpty(user)) {
            return new BigDecimal(0);
        }

        List<LogisticsFee> logisticsFeeList = logisticsFeeService.list(Wrappers.<LogisticsFee>lambdaQuery()
                .eq(LogisticsFee::getTenantId, user.getTenantId())
                .orderByDesc(LogisticsFee::getWeight)
        );
        for (LogisticsFee logisticsFee : logisticsFeeList) {
            List<String> areaStringList = Func.toStrList(",", logisticsFee.getMatchString());
            for (String areaString: areaStringList) {
                if (address.contains(areaString)) {
                    return logisticsFee.getFee();
                }
            }
        }
        return new BigDecimal(0);
    }
}
